<!-- 人员管理 -->
<template>
  <div>
    <!-- 模糊搜索表单 -->
    <el-form
      ref="jobs"
      :inline="true"
      :model="ruleForm"
      :rules="rule"
      class="demo-form-inline searchUser"
    >
      <el-form-item label="任意字段" prop="keyword">
        <el-input
          v-model="ruleForm.keyword"
          placeholder="请输入任意文字"
        ></el-input>
        <el-button type="primary" @click="
        search_Job({
          keyword:ruleForm.keyword,
          page:0,
          size:10
        })"
          ><i class="el-icon-search"></i><span>搜索</span></el-button
        >
      </el-form-item>
      <el-form-item>
        <el-button @click="resetForm('jobs')"
          ><i class="el-icon-refresh"></i><span>重置</span></el-button
        >
      </el-form-item>
    </el-form>
    <!-- 信息表格 -->
    <el-table :data="tableData" style="width: 100%">
      <el-table-column type="expand">
        <template slot-scope="scope">
          <el-form label-position="left" inline class="demo-table-expand">
            <el-form-item label="兼职岗位：">
              <span>{{ scope.row.title }}</span>
            </el-form-item>
            <el-form-item label="工作类型：">
              <span v-if="!scope.row.tags_string">暂无</span>
              <span v-else>{{ scope.row.tags_string }}</span>
            </el-form-item>
            <el-form-item label="详细介绍：">
              <span v-if="!scope.row.description">暂无</span>
              <span v-else>{{ scope.row.description }}</span>
            </el-form-item>
            <el-form-item label="地址：">
              <span v-if="!scope.row.formatted_address"></span>
              <span v-else>{{ scope.row.formatted_address }}</span>
            </el-form-item>
            <el-form-item label="人数：">
              <span v-if="!scope.row.available_count">暂无</span>
              <span v-else>{{ scope.row.available_count }}</span>
            </el-form-item>
            <el-form-item label="月薪：">
              <span v-if="!scope.row.salary">暂无</span>
              <span v-else>{{ scope.row.salary }}</span>
            </el-form-item>
            <el-form-item label="时薪：">
              <span v-if="!scope.row.wages">暂无</span>
              <span v-else>{{ scope.row.wages }}</span>
            </el-form-item>
            <el-form-item label="发布者：">
              <span v-if="!scope.row.common_name">暂无</span>
              <span v-else>{{ scope.row.common_name }}</span>
            </el-form-item>
            <el-form-item label="联系方式：" v-if="scope.row.publisher">
              <span v-if="!scope.row.publisher.phone_number">暂无</span>
              <span v-else>{{ scope.row.publisher.phone_number }}</span>
            </el-form-item>
            <el-form-item label="电子邮箱：" v-if="scope.row.publisher">
              <span v-if="!scope.row.publisher.email">暂无</span>
              <span v-else>{{ scope.row.publisher.email }}</span>
            </el-form-item>
            <el-form-item label="发布时间：">
              <span v-if="!scope.row.created_time">暂无</span>
              <span v-else>{{ scope.row.created_time }}</span>
            </el-form-item>
            <el-form-item label="详情：" v-if="scope.row.images">
              <img
                v-for="(item, index) in scope.row.images"
                :key="index"
                :src="item"
                alt=""
                style="width: 100%"
              />
              <span v-if="scope.row.images[0] == 'string'">暂无</span>
            </el-form-item>
          </el-form>
        </template>
      </el-table-column>
      <el-table-column prop="title" label="兼职岗位" min-width="150">
      </el-table-column>
      <el-table-column prop="common_name" label="发布者" min-width="120">
      </el-table-column>
      <el-table-column label="地址" min-width="210" prop="formatted_address">
      </el-table-column>
      <el-table-column label="人数" min-width="70" prop="available_count">
      </el-table-column>
      <el-table-column label="工作类型" min-width="150" prop="tags_string">
      </el-table-column>
      <el-table-column label="操作" min-width="150">
        <template slot-scope="scope">
          <el-row>
            <el-button
              type="text"
              @click="del_Job(scope.row.id, scope.row.title)"
              ><i class="el-icon-delete"></i><span>删除</span></el-button
            >
          </el-row>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页-->
    <div class="block">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="page.number + 1"
        :page-sizes="[10, 20, 30, 50]"
        :page-size="page.size"
        layout="total, sizes, prev, pager, next, jumper"
        background
        :total="page.total_elements"
        v-if="page.total_elements != 0"
      >
      </el-pagination>
    </div>
  </div>
</template>
<script>
import { mapState, mapActions } from "vuex";

export default {
  data() {
    return {
      rule: {
        keyword: [
          { min: 2, message: "长度在不得小于2个字符", trigger: "change" },
        ],
      },
    };
  },
  mounted() {
    this["getData"]({
      page: 0,
      size: 10,
    });
  },
  methods: {
     ...mapActions("adminJob", [
      "getData",
      "getDatePage",
      "search_Job",
      "removeJob",
    ]),
    //改变每页数量时调用
    handleSizeChange(val) {
      this.$store.commit("adminJob/HANDLE_SIZE_CHANGE", { val });
      this["getDatePage"]({
        url: this["url_data"].url,
        params: this["url_data"].params,
      });
    },
    //改变页数时调用
    handleCurrentChange(val) {
      this.$store.commit("adminJob/HANDLE_CURRENT_CHANGE", { val });
      this["getDatePage"]({
        url: this["url_data"].url,
        params: this["url_data"].params,
      });
    },
    del_Job(id, name) {
      this.$confirm(`此操作将永久删除${name}, 是否继续?`, "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(async () => {
          let status = await this["removeJob"]({ id });
          if (status == 200) {
            this.$message({
              type: "success",
              message: "删除成功!",
            });
            setTimeout(() => {
              this["getDatePage"]({
                url: this["url_data"].url,
                params: this["url_data"].params,
              });
            }, 500);
          }
        })
        .catch((error) => {
          this.$message({
            type: "info",
            message: error,
          });
        });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
      this["getData"]({
        page: 0,
        size: 10,
      });
    },
  },
  computed: {
    ...mapState("adminJob",[
      "tableData",
      "ruleForm",
      "page", 
      "url_data"
    ]),
  },
  watch: {
    // 监听mapState中的变量，当数据变化（有值、值改变等），
    // 保证能拿到完整的数据，不至于存在初始化没有数据的问题，然后可以赋给本组件data中的变量
  },
};
</script>
<style  lang='scss' scoped>
@import "@/assets/css/adminPerson_y.scss";
</style>
